﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using SoccerScore.Compact.Linq;
using System.Windows.Forms;
using System.Configuration;

namespace Soccer_Score_Forecast
{
    public class SevenmLiveSingleToSql : ElementParserFunction, IDisposable
    {
        #region Implementing IDisposable and the Dispose Pattern Properly
        private bool disposed = false; // to detect redundant calls
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~SevenmLiveSingleToSql()
        {
            Dispose(false);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    // Free other state (managed objects).
                }
                // Free your own state (unmanaged objects).
                // Set large fields to null.
                disposed = true;
            }
        }
        #endregion
        private string status = null;
        private DateTime matchtime;
        private string matchtimestring;
        public SevenmLiveSingleToSql() { }
        public int InsertLiveHtmlTableToDB(string _html)
        {
            SqlAgilityPackTableXpath tbTag = new SqlAgilityPackTableXpath("live.win.7m.cn");
            HtmlAgilityPackGeneric SevenmLivesg = new HtmlAgilityPackGeneric(_html, tbTag.tbTag, 0);//容易出错，主要路径的转换
            DataTable dt = SevenmLivesg.GetTableOutHtml(); //容易出错，注意方法的转换
            DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn);
            //if (!Conn.CreateTable(typeof(Live_Single))) { return 0; }
            foreach (DataRow aa in dt.Rows)
            {
                if (aa[0] != null)
                    if (aa[0].ToString() != "")
                        if (aa[0].ToString().Trim() != "")
                            if (aa[0].ToString().IndexOf("年") != -1)
                                if (aa[0].ToString().IndexOf("月") != -1)
                                    if (aa[0].ToString().IndexOf("日") != -1)
                                    {
                                        matchtimestring = HtmlDateFromBjSingle(aa[0].ToString());
                                        if (matchtimestring.Length >= 11)
                                            DateTime.TryParse(matchtimestring.Substring(0, 11), out matchtime);
                                    }

                if (aa[0] != null)
                    if (aa[0].ToString() != "")
                        if (aa[0].ToString().Trim() != "")
                        {
                            status = HtmlTextToStr(aa[0].ToString()).Trim();
                            if (status == "直播" || status == "最新赛果") continue;
                            //北京单场
                            if (status != null)
                            {
                                if (status.Length >= 1 && status.Length <= 5)
                                {
                                    if (dt.Columns.Count > 13)
                                    {
                                        Live_Single ls = new Live_Single();
                                        status = HtmlTextToStr(aa[9].ToString()).Trim();
                                        ls.Html_position = GetNumber(HtmlTextToStr(aa[0].ToString()));
                                        ls.Home_team_big = GetNumber(HtmlHrefToStr(aa[4].ToString()));
                                        ls.Away_team_big = GetNumber(HtmlHrefToStr(aa[6].ToString()));

                                        //ls.Status = status;
                                        ls.half_score = HtmlTextToStr(aa[7].ToString());
                                        ls.full_score = HtmlTextToStr(aa[5].ToString());

                                        //ls.Bj_odds = HtmlTextToStr(aa[8].ToString());  //让球列,修正北京单场的问题
                                        string bj = HtmlTextToStr(aa[8].ToString());
                                        string odds = HtmlTextToStr(aa[9].ToString());
                                        ls.Bj_odds = HyOddsBj(odds, bj);

                                        ls.Bj_result = HtmlTextToStr(aa[13].ToString());//彩果列
                                        ls.match_time = matchtime.Date;
                                        ls.match_type = HtmlToMatch(aa[1].ToString());

                                        if (ls.match_type != null)
                                            match.Live_Single.InsertOnSubmit(ls);
                                    }
                                }
                            }


                            //竞猜
                            if (aa[8] != null)
                                if (aa[8].ToString() != "")
                                    if (aa[8].ToString().Trim() != "")
                                    {
                                        string odds = HtmlTextToStr(aa[8].ToString()).Trim();
                                        if (odds.IndexOf(".") != -1)
                                        {
                                            Live_Single ls = new Live_Single();
                                            ls.Html_position = GetNumber(HtmlTextToStr(aa[0].ToString()));
                                            ls.Home_team_big = GetNumber(HtmlHrefToStr(aa[4].ToString()));
                                            ls.Away_team_big = GetNumber(HtmlHrefToStr(aa[6].ToString()));
                                            ls.Bj_odds = ConvertOddsToAsia(odds);

                                            ls.match_time = matchtime.Date;
                                            ls.match_type = HtmlToMatch(aa[1].ToString());

                                            match.Live_Single.InsertOnSubmit(ls);
                                        }
                                    }

                        }
            }
            match.SubmitChanges();
            return match.Live_Single.Max(e => e.Live_Single_id);
        }

        double hwin = Convert.ToDouble(ConfigurationManager.AppSettings["hwin"].ToString());
        double hlose = Convert.ToDouble(ConfigurationManager.AppSettings["hlose"].ToString());
        double cost = Convert.ToDouble(ConfigurationManager.AppSettings["cost"].ToString());

        public string HyOddsBj(string odds, string bjsingle)
        {
            int odd = Convert.ToInt32(ConvertOddsToAsia(odds));
            int bj = Convert.ToInt32(bjsingle);
            int hyoddsbj = odd + bj;
            return hyoddsbj.ToString();
        }

        public string ConvertOddsToAsia(string odds)
        {
            double odd = 0;
            string asia = "0";
            if (odds != null)
                if (odds != string.Empty)
                {
                    double.TryParse(odds, out odd);
                    if (odd < (1 / hwin) * cost)
                        asia = "-1";
                    if (odd > (1 / hlose) * cost)
                        asia = "1";
                }
            return asia;
        }

        public int InsertResultHtmlTableToDB(string _html)
        {
            SqlAgilityPackTableXpath tbTag = new SqlAgilityPackTableXpath("live.win.7m.cn");
            HtmlAgilityPackGeneric SevenmLivesg = new HtmlAgilityPackGeneric(_html, tbTag.tbTag, 0);//容易出错，主要路径的转换
            DataTable dt = SevenmLivesg.GetTableOutHtml(); //容易出错，注意方法的转换
            DataClassesMatchDataContext match = new DataClassesMatchDataContext(Conn.conn);
            //if (!Conn.CreateTable(typeof(Live_Single))) { return 0; }
            foreach (DataRow aa in dt.Rows)
            {
                if (aa[0] != null)
                    if (aa[0].ToString() != "")
                        if (aa[0].ToString().Trim() != "")
                            if (aa[0].ToString().IndexOf("年") != -1)
                            {
                                matchtimestring = HtmlDateFromBjSingle(aa[0].ToString());
                                if (matchtimestring.Length >= 11)
                                    DateTime.TryParse(matchtimestring.Substring(0, 11), out matchtime);
                            }

                if (aa[0] != null)
                    if (aa[0].ToString() != "")
                        if (aa[0].ToString().Trim() != "")
                        {
                            status = HtmlTextToStr(aa[0].ToString()).Trim();

                            //北京单场
                            if (status != null)
                            {
                                if (status.Length >= 1 && status.Length <= 3)
                                {
                                    Live_Single ls = new Live_Single();
                                    status = HtmlTextToStr(aa[9].ToString()).Trim();
                                    ls.Html_position = GetNumber(HtmlTextToStr(aa[0].ToString()));
                                    ls.Home_team_big = GetNumber(HtmlHrefToStr(aa[3].ToString()));
                                    ls.Away_team_big = GetNumber(HtmlHrefToStr(aa[5].ToString()));


                                    //ls.Status = status;
                                    ls.half_score = HtmlTextToStr(aa[6].ToString());
                                    ls.full_score = HtmlTextToStr(aa[4].ToString()).Replace("&nbsp;", "");
                                    ls.Bj_odds = HtmlTextToStr(aa[7].ToString());
                                    ls.Bj_result = HtmlTextToStr(aa[9].ToString());
                                    ls.match_time = matchtime.Date;
                                    ls.match_type = HtmlToMatch(aa[1].ToString());

                                    match.Live_Single.InsertOnSubmit(ls);
                                }
                            }

                        }
            }
            match.SubmitChanges();
            return match.Live_Single.Max(e => e.Live_Single_id);
        }
    }
}
